package com.smallkingson.ylib.core.utils;

import com.smallkingson.ylib.core.Yr;

import java.util.ArrayList;
import java.util.List;

public class YTimeTest {

	private List<YTimeHolder> holders;

	private String timeFormat = "yyyy.MM.dd HH:mm:ss";

	public YTimeTest() {
		holders = new ArrayList<YTimeHolder>();
	}

	public void test() {

		test(null);
	}

	public void test(String log) {

		YTimeHolder holder = new YTimeHolder();

		long currentTimeMillis = System.currentTimeMillis();

		holder.setTimeStamp(currentTimeMillis);

		holder.setTime(YStrings.formatTimeStamp(currentTimeMillis, timeFormat));

		StackTraceElement stack[] = new Throwable().getStackTrace();
		StackTraceElement stackMsg = log == null ? stack[2] : stack[1];
		String className = stackMsg.getClassName();
		className = className.substring(className.lastIndexOf(".") + 1);
		holder.setMsg("Thread: "
				+ Thread.currentThread().getName() + ", " + className + "."
				+ stackMsg.getMethodName() + " line: "
				+ stackMsg.getLineNumber() + (log == null ? "" : "-->" + log));

		holders.add(holder);
	}

	public void print() {

		for (YTimeHolder h : holders) {
			Yr.d(h.toString());
		}
		holders.clear();
	}

	public void setTimeFormat(String timeFormat) {

		this.timeFormat = timeFormat;
	}

	public static class YTimeHolder {

		private String time;

		private long timeStamp;

		private String msg;

		public void setTimeStamp(long timeStamp) {
			this.timeStamp = timeStamp;
		}

		public void setTime(String time) {
			this.time = time;
		}

		public String getTime() {
			return time;
		}

		public String getMsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		@Override
		public String toString() {
			return "[YTimeTest] time=" + time + ", timeStamp=" + timeStamp
					+ ", msg=" + msg;
		}
	}
}
